package com.lixinkeji.widget

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.widget.RelativeLayout
import com.airbnb.lottie.LottieAnimationView
import com.lixinkeji.extension.dp

import com.scwang.smart.refresh.layout.api.RefreshKernel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.constant.RefreshState
import com.scwang.smart.refresh.layout.constant.SpinnerStyle

import com.scwang.smart.refresh.layout.api.RefreshHeader


/**
 *
 * @ClassName LoadingHeader
 * @Author lwc
 * @Email 1679114787@qq.com
 * @Date 2022/1/3 16:51
 *
 */
class LoadingHeader : RelativeLayout, RefreshHeader {
    private var lottieAnimationView: LottieAnimationView? = null

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    init {
        lottieAnimationView = LottieAnimationView(context)
        lottieAnimationView?.loop(true)
        lottieAnimationView?.setAnimation("loading.json")
        val layoutParams = LayoutParams(100.dp, 50.dp)
        lottieAnimationView?.speed = 1.0f
        layoutParams.addRule(CENTER_IN_PARENT, TRUE)
        addView(lottieAnimationView, layoutParams)
    }

    override fun onStateChanged(
        refreshLayout: RefreshLayout,
        oldState: RefreshState,
        newState: RefreshState
    ) {
        Log.e("TAG", "onStateChanged: $newState")
        when (newState) {
            //每次重新下拉时，将图片资源重置为小人的大脑袋
            RefreshState.PullDownToRefresh -> {
                lottieAnimationView?.cancelAnimation()
                lottieAnimationView?.playAnimation()
            }
            else -> {

            }
        }

    }

    override fun getView(): View {
        return this
    }

    override fun getSpinnerStyle(): SpinnerStyle {
        return SpinnerStyle.Translate;
    }

    override fun setPrimaryColors(vararg colors: Int) {

    }

    override fun onInitialized(kernel: RefreshKernel, height: Int, maxDragHeight: Int) {

    }

    override fun onMoving(
        isDragging: Boolean,
        percent: Float,
        offset: Int,
        height: Int,
        maxDragHeight: Int
    ) {

    }

    override fun onReleased(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {

    }

    override fun onStartAnimator(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {

    }

    override fun onFinish(refreshLayout: RefreshLayout, success: Boolean): Int {
        /* if (lottieAnimationView.isAnimating){
             lottieAnimationView.cancelAnimation()
         }*/
        return 0;
    }

    override fun onHorizontalDrag(percentX: Float, offsetX: Int, offsetMax: Int) {

    }

    override fun isSupportHorizontalDrag(): Boolean {
        return false
    }
}